package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.baseConfig.queryBean.MeterModelQueryBean;
import com.koron.css2.baseConfig.vo.MeterModelVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

public class MsMeterModelSelect implements ServerInterface{

	private static Logger log = LogManager.getLogger(MsChgQueryNew.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			MeterModelQueryBean query = JsonUtils.objectToPojo(req.getData(), MeterModelQueryBean.class);
			//水表厂商
            MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
            MeterModelMapper mapper = factory.getMapper(MeterModelMapper.class);
			List<MeterModelVO> modelList = mapper.selectMeterModelList(query);
			List<Map<String, String>> result = modelList.stream().map(t -> {
				Map<String, String> data = new HashMap<String, String>(); 
				MeterFactoryBean meterFactory = meterFactoryMapper.selectMeterFactoryById(t.getFactoryName());
				String fullName = "型号：" + t.getModelNo() + " 名称：" + t.getModelName() +  (meterFactory == null ? "" : " 厂家：" + meterFactory.getFactoryFullName());
				data.put("id", t.getId());
				data.put("value", fullName);
				data.put("name", t.getModelName());
				return data;
			}).collect(toList());
			info.setData(result);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询水表型号失败");
			log.error("查询水表型号失败", e);
		}
		return info;
	}
}
